
const path = require('path');
var MinCssExtractPlugin = require('mini-css-extract-plugin');
var HtmlWebpackPlugin = require("html-webpack-plugin");

// 环境变量
var WEBPACK_DEV = process.env.WEBPACK_DEV || 'dev';
console.log(WEBPACK_DEV);

var getHtmlConfig = function(name, title){
  return {
    template : __dirname + '/src/view/' + name + '.html',
    filename : 'view/' + name + '.html',
    title : title,
    inject : true,
    hash : true,
    chunks : ['common',name]

  };

};

var config = {
  entry :{
    'common' : ['./src/page/common/index.js'],
    'index' : ['./src/page/index/index.js'],
    'login' : ['./src/page/login/index.js'],
    'result' : ['./src/page/result/index.js'],
  },
  output : {
    path: path.resolve(__dirname, 'dist'),
    publicPath:'/dist',
    filename : 'js/[name].js'
  },
  // 本地调试跨域问题
  devServer:{
    port:80,
    proxy :{
      '/product' :{
        target : "http://happymmall.com/",
        changeOrigin:true,
        secure:false
      }
    }
  },

  resolve :{
    alias : {
      node_modules  : __dirname + '/node_modules',
      util          : __dirname + '/src/util',
      page          : __dirname + '/src/page',
      service       : __dirname + '/src/service',
      image         : __dirname + '/src/images'
    }


  },
  plugins :[ 
    // css单独打包
    new MinCssExtractPlugin(
      {filename : 'css/[name].css',}
    ),
    new HtmlWebpackPlugin(getHtmlConfig('index','首页')),
    new HtmlWebpackPlugin(getHtmlConfig('login','用户登录')),
    new HtmlWebpackPlugin(getHtmlConfig('result', '操作结果')),

  ],
  externals: {
    'jquery' : 'window.jQuery'
  },
  // optimization : {
  //   splitChunks:{ // 设置分离代码的参数,符合条件的将会被打包进公共文件
  //     cacheGroups :{ // 设置缓存组用来提取满足不同规则的chunk
  //       common:{
  //         name: 'common',
  //         chunks:'all',
  //         minSize:1,
  //         minChunks:1,
  //         priority:1 // 设置匹配优先级
  //       }
  //     }
  //   }
  // },
  module: {
    rules: [
      { test : /\.css$/, use : [MinCssExtractPlugin.loader,'css-loader'] },
      { test : /\.(gif|png|jpg|jpeg|woff|svg|eot|ttf)\??.*$/, use : {loader:'url-loader', options:{limit:10, name: '[name].[ext]',outputPath:'resource/',publicPath:'../resource/'}}},
      { test : /\.string$/, use : [ 'html-loader'] },
    ]
  }

};

module.exports = config;